import 'package:flutter/material.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// Descriptions: 确认弹窗
/// User: tangzhentao
/// Date: 4:58 下午 2021/12/7
///
/// [title] 标题 默认：提示
///
/// [content] 内容 空值不显示
///
/// [notice] 着重提示 空值不显示
///
/// [cancel] 取消按钮 默认：取消
///
/// [sure] 确认按钮 默认：确认
///
///
///
Future<bool?> showConfirmDialog({
  String? title,
  String? content,
  String? notice,
  String cancel = '',
  String sure = '确认',
  Color contentColor = XfgColor.text1D,
  bool barrierDismissible = true,
}) async {
  return await showDialog<bool>(
      context: XfgNav.currentContext,
      barrierDismissible: barrierDismissible,
      builder: (_) => _ConfirmDialog(
          title: title,
          content: content,
          notice: notice,
          contentColor: contentColor,
          cancel: cancel,
          sure: sure
      )
  );
}

class _ConfirmDialog extends Dialog {
  final String? title;
  final String? content;
  final String? notice;
  final String cancel;
  final String sure;
  final Color contentColor;

  const _ConfirmDialog({this.title, this.content, this.notice, required this.cancel, required this.sure, required this.contentColor});

  @override
  Widget build(BuildContext context) {
    final Widget bottom;
    final sureWidget = InkWell(
      onTap: () => XfgNav.pop(true),
      child: Container(height: XfgSize().size55, alignment: Alignment.center, child: XfgText(sure, fontSize: XfgSize().text20, color: XfgColor.theme, bold: FontWeight.bold)),
    );
    if (cancel.isNotEmpty) {
      bottom = Row(
        children: [
          Expanded(
            child: InkWell(
              onTap: () => XfgNav.pop(false),
              child: Container(height: XfgSize().size55, alignment: Alignment.center, child: XfgText(cancel, fontSize: XfgSize().text20, color: XfgColor.text3, bold: FontWeight.bold)),
            ),
          ),
          strokeWidthContainer,
          Expanded(child: sureWidget)
        ],
      );
    } else {
      bottom = sureWidget;
    }
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: XfgSize().size325,
          decoration: white6Decoration,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              Visibility(
                visible: title?.isNotEmpty == true,
                child: Padding(
                  padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size22, XfgSize().size10, 0),
                  child: XfgText(title?? '', fontSize: XfgSize().text20, color: XfgColor.text1D, bold: FontWeight.bold),
                ),
              ),
              Visibility(
                visible: content?.isNotEmpty == true,
                child: Padding(
                  padding: EdgeInsets.fromLTRB(XfgSize().size44, XfgSize().size22, XfgSize().size44, 0),
                  child: XfgText(content?? '', fontSize: XfgSize().text14, color: XfgColor.text1D, height: 1.5, textAlign: TextAlign.center),
                ),
              ),
              Visibility(
                visible: notice?.isNotEmpty == true,
                child: Padding(
                  padding: EdgeInsets.fromLTRB(XfgSize().size44, XfgSize().size22, XfgSize().size44, 0),
                  child: XfgText(notice?? '', fontSize: XfgSize().text14, color: XfgColor.text1D, height: 1.5, bold: FontWeight.bold, textAlign: TextAlign.center),
                ),
              ),
              SizedBox(height: XfgSize().size24),
              strokeHeightContainer,
              bottom
            ],
          ),
        ),
      ),
    );
  }
}